﻿namespace Admin.NET.Application.Service;

/// <summary>
/// 输血指征
/// </summary>
public class BasShuXueZhiZhengService : BaseService<BasShuXueZhiZheng>
{
    public BasShuXueZhiZhengService(IServiceScopeFactory scopeFactory) : base(scopeFactory)
    {
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    public async Task<SqlSugarPagedList<BasShuXueZhiZhengOutput>> Page(BaseKeywordPageInput input)
    {
        return await Queryable
           .WhereIF(input.Keyword.HasValue(), it => it.mingCheng.Contains(input.Keyword))
           .Select<BasShuXueZhiZhengOutput>()
           .OrderBy(it => new { it.orderNo })
           .ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 用于缓存
    /// </summary>
    public List<BasShuXueZhiZhengAllOutput> All()
    {
        return _rep.Queryable
            .Select<BasShuXueZhiZhengAllOutput>()
            .OrderBy(it => new { it.orderNo })
            .ToList();
    }

    /// <summary>
    /// 新增成功后返回主键
    /// </summary>
    public long Add(BasShuXueZhiZhengInput input)
    {
        if (input.pId != -1)
        {
            UpdateFather(input.pId);
        }
        return AddWithInput(input);
    }

    /// <summary>
    /// 更新成功后返回true
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public bool Update(BasShuXueZhiZhengInput input)
    {
        if (input.pId != -1)
        {
            UpdateFather(input.pId);
        }
        return UpdateWithInput(input);
    }

    public bool UpdateFather(long? id)
    {
        var entity = _rep.Queryable.First(e => e.Id == id);
        if (entity == null)
        {
            return false;
        }
        entity.pId = 0;
        return UpdateWithInput(entity);
    }
}
